﻿using System;
using System.ComponentModel;
using System.Globalization;

namespace SpringSeries.Core.CustomTypeConverter
{
    public class AddressInfoConverter : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context,
                                            Type sourceType)
        {
            if (sourceType == typeof(string))
            {
                return true;
            }
            return base.CanConvertFrom(context, sourceType);
        }

        public override object ConvertFrom(ITypeDescriptorContext context,
                                            CultureInfo culture, object value)
        {
            if (value is string)
            {
                string[] v = ((string)value).Split(new char[] { ',' });
                return new AddressInfo(v[0], v[1], v[2]);
            }
            return base.ConvertFrom(context, culture, value);
        }
    }
}
